﻿Public Class FormMain
    Dim timerTask As Timer '任务
    Dim timerTask_interval As Integer = 2000 '时间间隔 
    Dim objClsSysParam As New clsSysParam
    Dim agvswitchBool As Boolean = False
    Dim objClsTask As New clsTask
    Dim dtTask As DataTable
    Dim objClsTaskStep As New clsTaskStep
    Dim dtTaskStep As DataTable
    Dim objClsTaskRecord As New clsTaskRecord
    Dim objClsLhPgd As New clsLhPgd
    Dim objClsHWSetting As New clsHWSetting
    Dim objClsProdInfo As New clsProdInfo
    Dim objClsRoute As New clsRoute
    Dim objLhCacheArea As New clsLhCacheArea
    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        connectDB()
        initTimer()
        checkAgvSwitch()
        initTask()
        If agvswitchBool Then
            initPlan2Task()
        End If
    End Sub

    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\Server_AGV.ini"

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If

    End Sub

    Private Sub checkAgvSwitch()
        Dim dt As DataTable = objClsSysParam.selectAgvSwitch()
        If dt.Rows.Count = 1 Then
            If dt.Rows(0)("param_value").ToString().Equals("1") Then
                agvswitchBool = True
            Else
                agvswitchBool = False
            End If
        End If
    End Sub

    Private Sub initTimer()
        timerTask = New Timer
        timerTask.Interval = timerTask_interval
        AddHandler timerTask.Tick, AddressOf timerTask_Tick
        timerTask.Start()
        btnTimer.Text = "timer停止"
    End Sub

    Private Sub timerTask_Tick(sender As Object, e As EventArgs)
        Try
            checkAgvSwitch()
            initTask()
            If agvswitchBool Then
                initPlan2Task()
            End If
        Catch ex As Exception
            timerTask.Stop()
            MsgBox("与服务器断开连接，请检查网络状态。")
            End 'Application.Restart()
        End Try

    End Sub
    ''' <summary>
    ''' 任务迁移
    ''' </summary>
    Private Sub initTask()
        If agvswitchBool Then
            dtTask = New DataTable()
            dtTask = objClsTask.SelectTask()
            dgvTask.DataSource = dtTask
            dtTaskStep = objClsTaskStep.SelectTaskStep()
            dgvTaskStep.DataSource = dtTaskStep
            For index = 0 To dtTask.Rows.Count - 1
                If (dtTask.Rows(index)("status").ToString().Equals("已完成2") And
                    dtTask.Rows(index)("tm").ToString().Equals("0")) Or
                    dtTask.Rows(index)("status").ToString().Equals("已取消") Then
                    '执行数据迁移的功能
                    Dim en_task_record As New entity_task_record
                    en_task_record.sys_id = dtTask.Rows(index)("sys_id")
                    en_task_record.type = dtTask.Rows(index)("type")
                    en_task_record.prod_id = dtTask.Rows(index)("prod_id")
                    en_task_record.status = dtTask.Rows(index)("status")
                    If String.IsNullOrEmpty(dtTask.Rows(index)("start_time").ToString()) Then
                        en_task_record.start_time = DBNull.Value.ToString()
                    Else
                        en_task_record.start_time = dtTask.Rows(index)("start_time")
                    End If
                    If String.IsNullOrEmpty(dtTask.Rows(index)("end_time").ToString()) Then
                        en_task_record.end_time = DBNull.Value.ToString()
                    Else
                        en_task_record.end_time = dtTask.Rows(index)("end_time")
                    End If

                    en_task_record.update_time = dtTask.Rows(index)("update_time")
                    en_task_record.tm = dtTask.Rows(index)("tm")
                    en_task_record.lhj = dtTask.Rows(index)("lhj")
                    en_task_record.prod_spec_id = dtTask.Rows(index)("prod_spec_id")
                    Dim en_task_step_record1 As New entity_task_step_record
                    Dim en_task_step_record2 As New entity_task_step_record
                    Dim dttstep As DataTable = objClsTask.SelectTaskStepByTaskId(dtTask.Rows(index)("sys_id"))
                    For idx = 0 To dttstep.Rows.Count - 1
                        If String.IsNullOrEmpty(dttstep.Rows(idx)("previous_id").ToString()) Then
                            en_task_step_record1.sys_id = dttstep.Rows(idx)("sys_id")
                            en_task_step_record1.prio = dttstep.Rows(idx)("prio")
                            en_task_step_record1.pick = dttstep.Rows(idx)("pick")
                            en_task_step_record1.drop = dttstep.Rows(idx)("drop")
                            en_task_step_record1.agv = dttstep.Rows(idx)("agv")
                            en_task_step_record1.task_id = dttstep.Rows(idx)("task_id")
                            en_task_step_record1.status = dttstep.Rows(idx)("status")
                            en_task_step_record1.prod_id = dttstep.Rows(idx)("prod_id")
                            en_task_step_record1.update_time = dttstep.Rows(idx)("update_time")
                            en_task_step_record1.lhj = dttstep.Rows(idx)("lhj")
                            en_task_step_record1.prod_spec_id = dttstep.Rows(idx)("prod_spec_id")
                        End If
                        If String.IsNullOrEmpty(dttstep.Rows(idx)("previous_id").ToString()) = False Then
                            en_task_step_record2.sys_id = dttstep.Rows(idx)("sys_id")
                            en_task_step_record2.prio = dttstep.Rows(idx)("prio")
                            en_task_step_record2.pick = dttstep.Rows(idx)("pick")
                            en_task_step_record2.drop = dttstep.Rows(idx)("drop")
                            en_task_step_record2.agv = dttstep.Rows(idx)("agv")
                            en_task_step_record2.task_id = dttstep.Rows(idx)("task_id")
                            en_task_step_record2.status = dttstep.Rows(idx)("status")
                            en_task_step_record2.prod_id = dttstep.Rows(idx)("prod_id")
                            en_task_step_record2.previous_id = dttstep.Rows(idx)("previous_id")
                            en_task_step_record2.update_time = dttstep.Rows(idx)("update_time")
                            en_task_step_record2.lhj = dttstep.Rows(idx)("lhj")
                            en_task_step_record2.prod_spec_id = dttstep.Rows(idx)("prod_spec_id")
                        End If
                    Next
                    objClsTaskRecord.save(en_task_record, en_task_step_record1, en_task_step_record2)
                End If
            Next
            dtTask = objClsTask.SelectTask()
            dgvTask.DataSource = dtTask
            dtTaskStep = objClsTaskStep.SelectTaskStep()
            dgvTaskStep.DataSource = dtTaskStep
        Else
            dgvTask.DataSource = New DataTable
            dgvTaskStep.DataSource = New DataTable
        End If
    End Sub
    ''' <summary>
    ''' 任务生成
    ''' </summary>
    Private Sub initPlan2Task()
        '查看所有硫化机
        Dim dtLHJ = objClsHWSetting.selectAllLHJ()
        '查看硫化机生产计划
        Dim dt As DataTable = objClsLhPgd.getLhPgd()
        For index = 0 To dtLHJ.Rows.Count - 1
            Dim dr As DataRow() = dt.Select("lhj like '%" & dtLHJ.Rows(index)("equip_id") & "%'")
            Dim dtSysParam As DataTable = objClsSysParam.selectLH_Out_Warning_Minute()
            If dtLHJ.Rows(index)("status").ToString().Equals("3") Then '硫化机待出炉
                If Integer.Parse(dtLHJ.Rows(index)("last_minutes").ToString()) <= Int32.Parse(dtSysParam.Rows(0)("param_value").ToString()) And
                        Integer.Parse(dtLHJ.Rows(index)("last_minutes").ToString()) >= 0 Then
                    If dr.Length > 0 Then
                        If Decimal.Parse(dr(0)("oper_count").ToString()) - Decimal.Parse(dr(0)("lhj_finish_count").ToString()) > 0 Then
                            '出入炉
                            setEntity(dtLHJ.Rows(index)("type_name").ToString(), dtLHJ.Rows(index)("equip_id").ToString(),
                                      "", dtLHJ.Rows(index)("prod_id").ToString(),
                                      dtLHJ.Rows(index)("prod_spec_id").ToString(),
                                      dtLHJ.Rows(index)("prod_spec_id").ToString(),
                                      dtLHJ.Rows(index)("equip_id").ToString(),
                                      dr(0)("mes_project_name").ToString(), "出入炉")
                        ElseIf Decimal.Parse(dr(0)("oper_count").ToString()) - Decimal.Parse(dr(0)("lhj_finish_count").ToString()) = 0 Then
                            '只出炉
                            setEntity(dtLHJ.Rows(index)("type_name").ToString(), dtLHJ.Rows(index)("equip_id").ToString(),
                                      "", dtLHJ.Rows(index)("prod_id").ToString(),
                                      dtLHJ.Rows(index)("prod_spec_id").ToString(), dr(0)("support_model").ToString(),
                                      dtLHJ.Rows(index)("equip_id").ToString(),
                                      dr(0)("mes_project_name").ToString(), "出炉")
                        End If
                    Else
                        '只出炉
                        Dim dtProdInfo As DataTable = objClsProdInfo.SelectProdInfo(dtLHJ.Rows(index)("prod_id").ToString())
                        'Dim dtlhpgdd As DataTable = objClsLhPgd.getLhPgdById(dtProdInfo.Rows(0)("b_pgd_lh_details_sys_id").ToString())
                        setEntity(dtLHJ.Rows(index)("type_name").ToString(), dtLHJ.Rows(index)("equip_id").ToString(),
                                  "", dtLHJ.Rows(index)("prod_id").ToString(),
                                  dtLHJ.Rows(index)("prod_spec_id").ToString(), dtLHJ.Rows(index)("prod_spec_id").ToString(),
                                  dtLHJ.Rows(index)("equip_id").ToString(),
                                  "", "出炉")
                        'dtlhpgdd.Rows(0)("mes_project_name").ToString()
                    End If
                End If
            ElseIf dtLHJ.Rows(index)("status").ToString().Equals("0") Then
                If dr.Length > 0 Then
                    'If Decimal.Parse(dr(0)("lhj_finish_count").ToString()) = 0 Then
                    '只入炉
                    setEntity(dtLHJ.Rows(index)("type_name").ToString(), dtLHJ.Rows(index)("equip_id").ToString(),
                                  "", "",
                                  dr(0)("support_model").ToString(), "",
                                  dtLHJ.Rows(index)("equip_id").ToString(),
                                  dr(0)("mes_project_name").ToString(), "入炉")
                    'End If
                End If
            End If
        Next
    End Sub
    Private Function setEntity(type_name As String, equip_id As String,
                               prod_id_ru As String, prod_id_chu As String,
                               prod_spec_id_ru As String, prod_spec_id_chu As String,
                               lhj As String, mes_project_name As String, task_type As String) As Integer
        Dim rtn As Integer
        Dim dtTaskCheck As DataTable = objClsTask.SelectTaskCheck(lhj)
        If dtTaskCheck.Rows.Count = 0 Then
            Dim routeType As String
            If type_name.Equals("500T") Then
                routeType = "A"
            ElseIf type_name.Equals("1000T") Then
                routeType = "B"
            Else
                If String.IsNullOrEmpty(prod_spec_id_ru) = False Then
                    If prod_spec_id_ru.Contains("700") Or prod_spec_id_ru.Contains("500") Or prod_spec_id_ru.Contains("600") Then
                        routeType = "A"
                    Else
                        routeType = "B"
                    End If
                Else
                    If prod_spec_id_chu.Contains("700") Or prod_spec_id_chu.Contains("500") Or prod_spec_id_chu.Contains("600") Then
                        routeType = "A"
                    Else
                        routeType = "B"
                    End If
                End If
            End If
            Dim dtRoute As DataTable = objClsRoute.selectRouteByLhjAndType(equip_id, routeType)
            If dtRoute.Rows.Count > 0 Then
                Dim en_task As New entity_task
                en_task.type = task_type
                en_task.prod_id = prod_id_ru
                en_task.status = "待创建"
                If task_type.Equals("入炉") Then
                    en_task.tm = "0"
                    en_task.prod_spec_id = prod_spec_id_ru
                Else
                    en_task.tm = "1"
                    en_task.prod_spec_id = prod_spec_id_chu
                End If
                en_task.lhj = lhj
                en_task.mes_project_name = mes_project_name
                Dim en_task_step1 As New entity_task_step
                en_task_step1.prio = "0"
                en_task_step1.pick = dtRoute.Rows(0)("prod_point")
                en_task_step1.drop = dtRoute.Rows(0)("lhj_point")
                en_task_step1.agv = "0"
                en_task_step1.status = "待创建1"
                If task_type.Equals("入炉") Then
                    en_task_step1.prod_id = prod_id_ru
                ElseIf task_type.Equals("出入炉") Then
                    en_task_step1.prod_id = prod_id_ru
                Else
                    en_task_step1.prod_id = ""
                End If
                en_task_step1.lhj = lhj
                en_task_step1.prod_spec_id = prod_spec_id_ru
                Dim en_task_step2 As New entity_task_step
                en_task_step2.prio = "0"
                en_task_step2.pick = dtRoute.Rows(0)("lhj_point")
                If task_type.Equals("入炉") Then
                    en_task_step2.drop = dtRoute.Rows(0)("tm_point_n")
                Else
                    en_task_step2.drop = dtRoute.Rows(0)("tm_point_front")
                End If
                en_task_step2.agv = "0"
                en_task_step2.status = "待创建2"
                If task_type.Equals("入炉") Then
                    en_task_step2.prod_id = ""
                ElseIf task_type.Equals("出入炉") Then
                    en_task_step2.prod_id = prod_id_chu
                Else
                    en_task_step2.prod_id = prod_id_chu
                End If
                en_task_step2.lhj = lhj
                en_task_step2.prod_spec_id = prod_spec_id_chu
                rtn = objClsTask.save(en_task, en_task_step1, en_task_step2)
            Else
                rtn = -2
            End If
        Else
            rtn = -2
        End If
        Return rtn
    End Function

    Private Sub btnTimer_Click(sender As Object, e As EventArgs) Handles btnTimer.Click
        If btnTimer.Text.Equals("timer停止") Then
            timerTask.Stop()
            timerTask.Enabled = False
            btnTimer.Text = "timer启动"
            MsgBox("停止成功")
        Else
            timerTask.Start()
            timerTask.Enabled = True
            btnTimer.Text = "timer停止"
            MsgBox("启动成功")
        End If
    End Sub

    Private Sub btnTaskCancel_Click(sender As Object, e As EventArgs) Handles btnTaskCancel.Click
        If dgvTask.SelectedRows.Count > 0 Then
            Dim rtn As Integer = objClsTask.TaskCancel(dgvTask.SelectedRows(0).Cells("Colsys_id").Value.ToString())
            If rtn <> -2 Then
                MsgBox("取消成功")
            Else
                MsgBox("取消失败")
            End If
        End If
    End Sub

    Private Sub FormMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        If MsgBox("确认要进行关闭吗？", vbYesNo, "提示") = vbYes Then
            timerTask.Stop()
            timerTask.Enabled = False
        Else
            e.Cancel = True
        End If
    End Sub

    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub

End Class

Public Class entity_task
    Public type As String
    Public prod_id As String
    Public status As String
    Public tm As String
    Public lhj As String
    Public prod_spec_id As String
    Public mes_project_name As String
End Class

Public Class entity_task_step
    Public prio As String
    Public pick As String
    Public drop As String
    Public agv As String
    Public status As String
    Public prod_id As String
    Public lhj As String
    Public prod_spec_id As String
End Class

Public Class entity_task_record
    Public sys_id As String
    Public type As String
    Public prod_id As String
    Public status As String
    Public start_time As String
    Public end_time As String
    Public update_time As String
    Public tm As String
    Public lhj As String
    Public prod_spec_id As String
End Class

Public Class entity_task_step_record
    Public sys_id As String
    Public prio As String
    Public pick As String
    Public drop As String
    Public agv As String
    Public task_id As String
    Public status As String
    Public prod_id As String
    Public previous_id As String
    Public update_time As String
    Public lhj As String
    Public prod_spec_id As String
End Class